<template>
  <el-dialog
    append-to-body
    :close-on-click-modal="false"
    :visible.sync="dialogFormVisible"
    :before-close="close"
    :title="title"
    width="800px"
    :top="'10vh'"
  >
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="专项服务名称" prop="name" style="width: 100%">
            <el-input v-model="form.name" placeholder="请输入专项服务名称" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="专项服务简介" prop="fwjj" style="width: 100%">
            <el-input
              v-model="form.fwjj"
              type="textarea"
              maxlength="100"
              show-word-limit
              placeholder="请输入专项服务简介"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="服务说明" prop="zysx" style="width: 100%">
            <el-input
              v-model="form.zysx"
              type="textarea"
              maxlength="100"
              show-word-limit
              placeholder="请输入专项服务说明或者注意事项"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="背景图" prop="carouselImage">
            <div class="schoolBottomImage" @click="schoolBackgroundImage">
              <img v-if="form.backImage" :src="form.backImage" class="school-bak-image">
              <i v-else class="el-icon-plus avatar-uploader-icon" />
            </div>
            <el-input v-model="form.backImage" type="hidden" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="优先级" prop="px">
            <el-input-number v-model="form.px" :min="1" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="sfqy">
            <el-radio-group v-model="form.sfqy">
              <el-radio
                v-for="item in status"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="close">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save('form')">确认</el-button>
    </div>
    <!--  页脚背景图片剪切  -->
    <el-dialog :visible.sync="imageDialog" :append-to-body="true" width="70%">
      <cropper-image
        ref="child"
        :name="cropperName"
        :auto-crop-height="autoCropHeight"
        :auto-crop-width="autoCropWidth"
        :fixed-number="fixedNumber"
        @uploadImgSuccess="handleUploadSuccess"
      />
    </el-dialog>
  </el-dialog>
</template>

<script>
import { saveObject, updateObject } from '@/api/special/index'
import CropperImage from '@/common/cropper/index'
export default {
  name: 'SpecialEntity',
  components: { CropperImage },
  data() {
    return {
      dialogFormVisible: false,
      title: '专项服务管理',
      cropperName: 'cropperName',
      autoCropHeight: 50,
      autoCropWidth: 50,
      fixedNumber: [1, 1],
      imageDialog: false,
      loading: false,
      form: {
        sfqy: true
      },
      status: [{ id: 0, value: false, label: '否' },
        { id: 1, value: true, label: '是' }],
      rules: {
        name: [{ required: true, message: '请输入专项服务名称', trigger: 'blur' }],
        fwjj: [{ required: true, message: '请输入服务简介', trigger: 'blur' }],
        backImage: [{ required: true, message: '请上传背景图', trigger: 'change' }]
      }
    }
  },
  methods: {
    openEntity(data) {
      if (undefined === data || data === null) {
        this.title = '新增'
        this.dialogFormVisible = true
      } else {
        this.title = '编辑'
        this.form = Object.assign({}, data)
        this.dialogFormVisible = true
      }
    },
    save(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.form.id !== null && undefined !== this.form.id) {
            updateObject(this.form).then(res => {
              this.$message.success('更新成功！')
              this.close()
            }).catch(() => {
              this.$message.error('更新失败！')
            })
          } else {
            saveObject(this.form).then(res => {
              this.$message.success('保存成功！')
              this.close()
            }).catch(() => {
              this.$message.error('保存失败！')
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    close() {
      this.$refs['form'].resetFields()
      this.dialogFormVisible = false
      this.$emit('fetch-data')
    },
    schoolBackgroundImage() {
      this.fixedNumber = [2.7, 1]
      this.autoCropWidth = 250
      this.imageDialog = true
    },
    handleUploadSuccess(data) {
      this.form.backImage = data.url
      this.imageDialog = false
    }
  }
}
</script>

<style scoped>
.schoolBottomImage {
  width: 100%;
  height: 250px;
  border: 1px dashed #9b9b9b;
  border-radius: 4px;
  text-align: center;
  line-height: 250px;
  font-size: 34px;
}
.school-bak-image {
  width: 100%;
  height: 250px;
}
</style>
